#pragma once
#include <iostream>
#include <string>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

class InetAddr
{
public:
    InetAddr(struct sockaddr_in &addr)
        :_addr(addr)
    {
        _port = ntohs(_addr.sin_port);      
        // _ip = inet_ntoa(_addr.sin_addr);
        char buffer[INET_ADDRSTRLEN];   // 这个宏表示IPV4的地址大小
        inet_ntop(AF_INET, &addr.sin_addr, buffer, sizeof(buffer));
        _ip = buffer;    
    }

    std::string Ip() const { return _ip; }
    uint16_t Port() const { return _port;}

    struct sockaddr_in& GetAddr()
    {
        return _addr;
    }

    bool operator==(const InetAddr& addr) const
    {
        return this->_ip == addr.Ip() && this->_port == addr.Port();
    }

    std::string PrintDebug()
    {
        std::string info = _ip;
        info += ":";
        info += std::to_string(_port);
        return info;
    }

    ~InetAddr()
    {}

private:
    std::string _ip;    // ip
    uint16_t _port;     // 端口号

    struct sockaddr_in _addr; 
};